<?php
// 你的公共函数

/**
 * 树形数组 function
 *
 * @param string $dir 文件夹
 * @return array
 */
function getDirTree(string $dir, int $num = 0): array
{
    $tree = array();
    $files = scandir($dir);

    foreach ($files as $key => $value) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
        $is_file = false;
        if (is_file($path)) {
            $is_file = true;
        }
        $num++;
        if (!is_dir($path)) {
            $tree[] = array(
                "title" => $value,
                "is_file" => $is_file,
                "id" => str_replace(DIRECTORY_SEPARATOR, "@", $path),
                "key" => $num,
                "children" => array()
            );
        } elseif ($value != "." && $value != "..") {
            $subtree = getDirTree($path, $num);
            $tree[] = array(
                "title" => $value,
                "is_file" => $is_file,
                "id" => str_replace(DIRECTORY_SEPARATOR, "@", $path),
                "key" => $num,
                "children" => $subtree
            );
        }
    }

    return $tree;
}

/**
 * 删除文件或文件夹 function
 *
 * @param string $path
 * @return boolean
 */
function delete_folder_or_file(string $path): bool
{
    if (is_file($path)) {
        return unlink($path);
    } elseif (is_dir($path)) {
        $files = array_diff(scandir($path), array('.', '..'));
        foreach ($files as $file) {
            delete_folder_or_file("$path" . DIRECTORY_SEPARATOR . "$file");
        }
        return rmdir($path);
    }
    return false;
}
